﻿using RootMotion.FinalIK;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gta.Common;

public class PlayerAnimatorEvent : MonoBehaviour
{
    public GameObject MoneyObj;
    private List<GameObject> hasTakeThing = new List<GameObject>(32);
    public int MoneyId;
    public void TriggerMoneyInBag(int num)
    {
        if (button)
        {
            button.transform.localScale = Vector3.zero;
        }
        ResidentHandler.Inst.GetFunction("CityLuaFunction.OnMoneyInBag").Action<int,int>(MoneyId, num);
    }

    public InteractionSystem interactionSystem;
    public InteractionObject button;
    public bool interrupt;

    public InteractionObject.Message CallBack { get; private set; }

     public float flashDuration = 1.0f;
    public float flashFrequency = 0.1f;

    private IEnumerator FlashCoroutine()
    {
        float timer = 0;
        bool isVisible = true;

        while (timer < flashDuration)
        {
            // 切换可见性
            gameObject.SetActive(isVisible);
            isVisible = !isVisible;

            // 等待一段时间
            yield return new WaitForSeconds(flashFrequency);

            timer += flashFrequency;
        }

        // 确保最后角色是可见的
        gameObject.SetActive(true);
    }

    public void StartFlashing()
    {
        StartCoroutine(FlashCoroutine());
    }
    public void TriggerTakeThing()
    {
        if (MoneyObj!=null)
        {
            button = MoneyObj.GetComponentInChildren<InteractionObject>();
            if (button!=null)
            {
                hasTakeThing.Add(button.gameObject);
            }
        }
        interactionSystem.StartInteraction(FullBodyBipedEffector.RightHand, button, interrupt);
    }

    public void ResetMoneyObj()
    {
        if (MoneyObj!=null)
        {
            foreach (var thing in hasTakeThing)
            {
                thing.transform.SetParent(MoneyObj.transform);
            }
            GameObject.Destroy(MoneyObj);
        }
    }
}
